﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace YArchitech.MEP.Electric.CableLay
{
	public static class CableLayRevitCommon
	{
		public static void HighlightElement(UIDocument uiDoc, Element ent)
		{
			if (uiDoc == null || ent == null || !ent.IsValidObject)
			{
				return;
			}
			RevitVersionFuncs.ClearSelection(uiDoc.Selection);
			RevitVersionFuncs.AddToSelection(uiDoc.Selection, ent);
			uiDoc.ShowElements(ent.Id);
		}

		public static void HighlightElement(UIDocument uiDoc, List<Element> lstEnt)
		{
			if (uiDoc == null || lstEnt == null || lstEnt.Count == 0)
			{
				return;
			}
			List<Element> list = new List<Element>();
			List<ElementId> list2 = new List<ElementId>();
			foreach (Element element in lstEnt)
			{
				if (element != null && element.IsValidObject)
				{
					list.Add(element);
					list2.Add(element.Id);
				}
			}
			if (list.Count <= 0 || list2.Count <= 0)
			{
				return;
			}
			RevitVersionFuncs.SetSelection(uiDoc.Selection, list);
			uiDoc.ShowElements(list2);
		}

		public static ICollection<Element> CollectElectricalEquipment(Document doc)
		{
			if (doc == null)
			{
				return null;
			}
			ICollection<Element> collection = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).ToElements();
			if (collection == null || collection.Count == 0)
			{
				return null;
			}
			List<Element> list = new List<Element>();
			foreach (Element element in collection)
			{
				if (element != null)
				{
					FamilyInstance familyInstance = element as FamilyInstance;
					if (familyInstance != null && familyInstance.MEPModel != null && familyInstance.MEPModel.ConnectorManager != null)
					{
						ConnectorSet connectors = familyInstance.MEPModel.ConnectorManager.Connectors;
						if (connectors != null && connectors.Size > 0)
						{
							ConnectorSetIterator connectorSetIterator = connectors.ForwardIterator();
							while (connectorSetIterator.MoveNext())
							{
								if ((connectorSetIterator.Current as Connector).Domain == (Autodesk.Revit.DB.Domain)2)
								{
									list.Add(element);
									break;
								}
							}
						}
					}
				}
			}
			if (list.Count != 0)
			{
				return list;
			}
			return null;
		}

		public static Element GetEquipmentByNumber(Document doc, string number)
		{
			if (doc == null || string.IsNullOrWhiteSpace(number))
			{
				return null;
			}
			ICollection<Element> collection = CableLayRevitCommon.CollectElectricalEquipment(doc);
			if (collection == null || collection.Count == 0)
			{
				return null;
			}
			foreach (Element element in collection)
			{
				Parameter parameter = element.GetParameter("编号");
				if (parameter != null && parameter.AsString() == number)
				{
					return element;
				}
			}
			return null;
		}

		public static bool CableTraySchemaDataRemove(Document doc, CableListData data)
		{
			if (doc == null || data == null)
			{
				return false;
			}
			ICollection<ElementId> collection = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_CableTray).OfClass(typeof(CableTray)).ToElementIds();
			if (collection == null || collection.Count == 0)
			{
				return true;
			}
			Transaction transaction = new Transaction(doc, "RemoveLayCable");
			transaction.Start();
			bool result;
			try
			{
                //Predicate<CableTraySchemaData> <>9__0;
                //Predicate<CableTraySchemaData> <>9__1;
                //foreach (ElementId elementId in collection)
                //{
                //    Element element = doc.GetElement(elementId);
                //    if (element != null)
                //    {
                //        List<CableTraySchemaData> list = new List<CableTraySchemaData>();
                //        if (CableTraySchemaDataManager.GetData(element, ref list))
                //        {
                //            List<CableTraySchemaData> list2 = list;
                //            Predicate<CableTraySchemaData> match;
                //            if ((match = <>9__0) == null)
                //            {
                //                match = (<>9__0 = ((CableTraySchemaData item) => data.CableNum == item.CableNumber));
                //            }
                //            if (list2.Exists(match))
                //            {
                //                List<CableTraySchemaData> list3 = list;
                //                Predicate<CableTraySchemaData> match2;
                //                if ((match2 = <>9__1) == null)
                //                {
                //                    match2 = (<>9__1 = ((CableTraySchemaData item) => data.CableNum == item.CableNumber));
                //                }
                //                CableTraySchemaData item2 = list3.Find(match2);
                //                list.Remove(item2);
                //                if (list.Count <= 0)
                //                {
                //                    CableTraySchemaDataManager.DeleteSchemaData(element);
                //                }
                //                else
                //                {
                //                    CableTraySchemaDataManager.SetData(element, list);
                //                }
                //            }
                //        }
                //    }
                //}
				transaction.Commit();
				result = true;
			}
			catch
			{
                if (transaction != null && (int)(int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
				result = false;
			}
			return result;
		}

		public static bool CableTraySchemaDataRemove(Document doc, List<CableListData> lstData)
		{
			if (doc == null || lstData == null || lstData.Count <= 0)
			{
				return false;
			}
			ICollection<ElementId> collection = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_CableTray).OfClass(typeof(CableTray)).ToElementIds();
			if (collection == null || collection.Count == 0)
			{
				return true;
			}
			Transaction transaction = new Transaction(doc, "RemoveLayCable");
			transaction.Start();
			bool result;
			try
			{
				foreach (ElementId elementId in collection)
				{
					Element element = doc.GetElement(elementId);
					if (element != null)
					{
						List<CableTraySchemaData> list = new List<CableTraySchemaData>();
						if (CableTraySchemaDataManager.GetData(element, ref list))
						{
							using (List<CableListData>.Enumerator enumerator2 = lstData.GetEnumerator())
							{
								while (enumerator2.MoveNext())
								{
									CableListData data = enumerator2.Current;
									if (list.Exists((CableTraySchemaData item) => data.CableNum == item.CableNumber))
									{
										CableTraySchemaData item2 = list.Find((CableTraySchemaData item) => data.CableNum == item.CableNumber);
										list.Remove(item2);
									}
								}
							}
							if (list.Count <= 0)
							{
								CableTraySchemaDataManager.DeleteSchemaData(element);
							}
							else
							{
								CableTraySchemaDataManager.SetData(element, list);
							}
						}
					}
				}
				transaction.Commit();
				result = true;
			}
			catch
			{
                if (transaction != null && (int)(int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
				result = false;
			}
			return result;
		}

		public static bool CableTraySchemaDataRemove(Document doc)
		{
			if (doc == null)
			{
				return false;
			}
			ICollection<ElementId> collection = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_CableTray).OfClass(typeof(CableTray)).ToElementIds();
			if (collection == null || collection.Count == 0)
			{
				return true;
			}
			Transaction transaction = new Transaction(doc, "RemoveLayCable");
			transaction.Start();
			bool result;
			try
			{
				foreach (ElementId elementId in collection)
				{
					CableTraySchemaDataManager.DeleteSchemaData(doc.GetElement(elementId));
				}
				transaction.Commit();
				result = true;
			}
			catch (Exception)
			{
                if (transaction != null && (int)(int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
				result = false;
			}
			return result;
		}

		public static View Get3DView(UIDocument uiDoc)
		{
			if (uiDoc == null)
			{
				return null;
			}
			Document document = uiDoc.ActiveView.Document;
			View view = uiDoc.ActiveView;
			if (ViewUtils.IsViewType(view, CmdViewType.ThreeD))
			{
				return view;
			}
			List<ElementId> viewsOpened = ViewUtils.GetViewsOpened(uiDoc, CmdViewType.ThreeD);
			if (viewsOpened.Count > 0)
			{
				ElementId elementId = viewsOpened[0];
				view = (document.GetElement(elementId) as Autodesk.Revit.DB.View);
				if (view != null)
				{
					return view;
				}
			}
			foreach (ElementId elementId2 in ViewUtils.GetViews(document, CmdViewType.ThreeD))
			{
				view = (document.GetElement(elementId2) as Autodesk.Revit.DB.View);
				if (view != null)
				{
					return view;
				}
			}
			return null;
		}
	}
}
